package com.flight.trans.activity;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.EnterPortChannelingDetailsAdapter;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.ChannelBean.ChannelData;
import com.flight.trans.bean.EnterPickingBean;
import com.flight.trans.bean.EnterPortTakeBean.EnterPortTakeData;
import com.flight.trans.bean.EnterPortTakeDetailBean.EnterPortTakeDetailData;
import com.flight.trans.global.Constant;
import com.flight.trans.global.ConstantInfo;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.util.ToastUtils;
import com.flight.trans.view.dialog.AlertDialog;
import com.flight.trans.view.dialog.ChanelDialog;
import com.lzy.okgo.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 进港分配通道
 */
public class EnterPortChannelingDetailsActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mLlContainer ;
    private TextView mTvTitleName;
    private RecyclerView mRvContainer ;

    private Button mBtnConfirmChannel ;//确认分配

    private TextView mTvTakeNo ,//提货单号
            mTvCreatePerson ,//开单人
            mTvReceivingPersonnel ,//收货人
            mTvTakePersonnel ;//提货人
    private TextView mTvTakeRoute ;//分配通道

    private ChannelData mChannelData ;

    private EnterPortTakeData mTakeData ;
    private EnterPortChannelingDetailsAdapter mChannelingDetailsAdapter;
    private List<EnterPortTakeDetailData> mTakeDetailDataList ;

    @Override
    protected int setContentView() {
        return R.layout.activity_enter_port_channeling_details;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mRvContainer = findViewById(R.id.mRvContainer);

        mTvTakeRoute = findViewById(R.id.mTvTakeRoute);
        mBtnConfirmChannel = findViewById(R.id.mBtnConfirmChannel);

        mTvTakeNo = findViewById(R.id.mTvTakeNo);
        mTvCreatePerson = findViewById(R.id.mTvCreatePerson);
        mTvReceivingPersonnel = findViewById(R.id.mTvReceivingPerson);
        mTvTakePersonnel = findViewById(R.id.mTvTakePersonnel);
    }

    @Override
    protected void initData() {
        mTakeData = (EnterPortTakeData) mBundle.getSerializable(Constant.INFO);
        IsEditor = mBundle.getInt(Constant.IsEditor) != 2;

        ConstantInfo.UnusualIDList.clear();

        mTvTakeNo.setText(mTakeData.getConsigneeNo());
        mTvCreatePerson.setText(mTakeData.getConsigneeCreateByName());
        mTvReceivingPersonnel.setText(mTakeData.getReceiverName());
        mTvTakePersonnel.setText(mTakeData.getConsigneeName());

        mTvTitleName.setText(getString(R.string.title_enter_port_channeling));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);
    }

    @Override
    protected void initConnect() {
        valueParams = new HttpParams();
        valueParams.put(Constant.Consignee_No , mTakeData.getConsigneeNo());

        NetworkUtils.getEnterPortTakeDetails(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mTakeDetailDataList = (List<EnterPortTakeDetailData>) body;

                if(mTakeDetailDataList == null)
                    mTakeDetailDataList = new ArrayList<>();

                initAdapter();
            }
        }, valueParams);
    }

    private void initAdapter(){
        mBtnConfirmChannel.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

        if(mChannelingDetailsAdapter == null) {
            mChannelingDetailsAdapter = new EnterPortChannelingDetailsAdapter(mActivity, mTakeDetailDataList);
            mRvContainer.setAdapter(mChannelingDetailsAdapter);
            mRvContainer.setLayoutManager(new LinearLayoutManager(mActivity));
        }else {
            mChannelingDetailsAdapter.updateAdapter(mTakeDetailDataList);
        }
    }

    @Override
    public void onClick(View v) {
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (v.getId()){
            case R.id.mIbBack:
                onBackPressed();
                break;
            case R.id.mTvTakeRoute:
                if(mTakeDetailDataList == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                ChanelDialog.getInstance(mActivity)
                        .getChannelData(Constant.EnterPortTake)
                        .setOnItemClickListener(new ChanelDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(ChannelData channelData) {
                                mChannelData = channelData;
                                mTvTakeRoute.setText(channelData.getName());
                            }
                        }).show();
                break;
            case R.id.mBtnConfirmChannel:
                // 确认分配通道
                if(mTakeDetailDataList == null){
                    ToastUtils.showToast(R.string.toast_storeroom_inventory_data_null);
                    return;
                }

                if(mChannelData == null){
                    ToastUtils.showToast(R.string.toast_take_channel_null);
                    return;
                }

                showConfirmDialog();
                break;

        }
    }

    private void showConfirmDialog() {
        if(IsSaveIng){
            ToastUtils.showToast(R.string.toast_save_ing);
            return;
        }

        AlertDialog.getInstance(mActivity)
                .setContent(R.string.dialog_confirm_channel)
                .setConfirmButton(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        valueParams = new HttpParams();
                        valueParams.put(Constant.Consignee_No , mTakeData.getConsigneeNo());
                        valueParams.put(Constant.Delivery_Channel , mChannelData.getCode());

                        IsSaveIng = true;
                        NetworkUtils.saveEnterPortChannel(new OkGoBackListener(mActivity) {
                            @Override
                            public void onSuccess(Object body) {
                                IsSaveIng = false;
                                ToastUtils.showToast(R.string.toast_submit_success);
                                onFinish(Constant.RESULT_CODE);
                            }

                            @Override
                            public void onError() {
                                IsSaveIng = false;
                            }
                        }, valueParams);
                    }
                }).show();
    }

    @Override
    protected void initRefresh(int count) {
        super.initRefresh(count);

        NetworkUtils.checkPageLock(new OkGoBackListener(mActivity) , ConstantInfo.IN_STORAGE_OUT , mTakeData.getConsigneeNo());
    }
}
